// my/my.js
import {
  InvioceModel
} from '../../../models/invioce'
import {
  PayModel
} from '../../../models/pay'
import {
  Format
} from '../../../utils/common'
const payModel = new PayModel()
const invioceModel = new InvioceModel()
const app = getApp()
let last
const format = new Format()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    unBillAmount: '', //可开票金额
    showBottom: false, // 是否显示弹窗
    invoiceDefault: {InvoiceTitle: '',InvoiceMail: ''}, //获取默认列表
    invoiceLists: '', //发票抬头列表
    invoiceUnbilledLogIds: '', //选择发票列表id
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let a = options.invoiceUnbilledLogIds
    const arr=a.split(',')
    this.setData({
      'unBillAmount': options.unBillAmount,
      invoiceUnbilledLogIds: arr
    })
  },
  onShow: function () {
    console.log('1')
    this.getDefaultInvoice()
    // this.loadMyAccount()
  },
  // 获取默认接口
  getDefaultInvoice() {
    invioceModel.getDefaultInvoice().then(res => {
      console.log(res)
      if (res) {
        this.setData({
          'invoiceDefault': res
        })
        if (res.openTicketType) {
          this.setData({
            'openTicketType': res.openTicketType
          })
        }
      } else {
        my.showToast({
          type: 'none',
          content: '查询默认抬头失败',
          duration: 1000,
        });
      }
      
    })
  },
  onButtomBtnTap(e) {
    const index = e.currentTarget.dataset.index
    if (index == 1) {
      this.invoiceTileList()
    } else {
      this.getEmailList()
    }
    
  },
  onPopupClose() {
    this.setData({
      showBottom: false,
    });
  },
  invoiceTileList() {
    invioceModel.invoiceTileList().then(res => {
      console.log(res)
      if (res && res.code == '00') {
        this.setData({
          'invoiceLists': res.titles,
          showBottom: true,
          index: 1,
        })
      } else {
        if (res && res.msg) {
          my.showToast({
            type: 'none',
            content: res.msg,
            duration: 1000,
          });
        } else {
          my.showToast({
            type: 'none',
            content: '查询发票抬头失败',
            duration: 1000,
          });
        }
        
      }
      
    })
  },
  getEmailList() {
    invioceModel.getEmailList().then(res => {
      console.log(res)
      if (res && res.code == '00') {
        for (let i = 0; i < res.emailList.length; i++) {
          if(!res.emailList[i].mail){ 
    　　　　res.emailList.splice(i, 1);
            i--
      　　}
        }
        this.setData({
          'invoiceLists': res.emailList,
          showBottom: true,
          index: 2,
        })
      } else {
        if (res &&  res.code ) {
          my.showToast({
            type: 'none',
            content: res.msg,
            duration: 1000,
          });
        } else {
          my.showToast({
            type: 'none',
            content: "获取邮箱列表失败",
            duration: 1000,
          });
        }
      }
    })
  },
  selectInvoice (e) {
    const index = e.currentTarget.dataset.index
    let item =  e.currentTarget.dataset.item
    if (this.data.index == 1) {
      this.setData({
        'invoiceDefault.InvoiceTitle': item,
        showBottom: false,
      })
    } else {
      this.setData({
        'invoiceDefault.InvoiceMail': item,
        showBottom: false,
      })
    }
  },
  toOpen(e) {
    this.getBillAmountByTrades('next',0)
  },
  eleReceiptByTrades() {
    if (!this.data.invoiceDefault.InvoiceTitle) {
      my.showToast({
        type: 'none',
        content: '请选择默认抬头',
        duration: 1000,
      });
      return
    }
    if (!this.data.invoiceDefault.InvoiceMail.mail) {
      my.showToast({
        type: 'none',
        content: '请选择申请邮箱',
        duration: 1000,
      });
      return
    }
    invioceModel.eleReceiptByTrades(this.data.invoiceUnbilledLogIds,this.data.invoiceDefault.InvoiceTitle.id,this.data.invoiceDefault.InvoiceMail.mail,this.data.unBillAmount).then(res => {
      console.log(res)
      if (res && res.code == '00') {
        my.alert({
          content: '开票申请成功',
          buttonText: '知道了',
          success: () => {
            my.redirectTo({
              url: '/pages/invioce/invioceDownload/invioceDownload'
            })
          }
        });
      } else {
        if (res && res.msg) {
          my.showToast({
            type: 'none',
            content: res.msg,
            duration: 1000,
          });
        } else {
          my.showToast({
            type: 'none',
            content: '申请开票失败',
            duration: 1000,
          });
        }
        
      }
      
    })
  },
  cancal() {
    my.navigateBack()
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  // onShow: function () {
  //   // this.loadMyAccount()
  // },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})